<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;

class Cartoon extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $guanli = Session::get('adname');
        $data = db('zuopin')->select();
        return view('cartoon/cartoonlist',[
            'data'=>$data,
            'guanli'=>$guanli
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $time = time();
        $guanli = Session::get('adname');
//        dump($time);
        return view('cartoon/cartoonadd',[
            'guanli' => $guanli,
            'time' => $time
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->post();
//        dump($p);die;
        $file = request()->file('pic');
        //移动到框架应用根目录/public/uploads/ 目录下
        if ($file){
            $info = $file->validate(['size'=>156780,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
//            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info){
//                输出jpg
//                echo $info->getExtension();
//                输出20180802/...jpg
//                echo $info->getSaveName();
                $data['pic'] = $info->getSaveName();
//                输出...jpg
//                echo $info->getFilename();
            }else{
//                失败信息
                echo $file->getError();
            }
        }
        $res = db('zuopin')->data($data)->insert();

        if ($res > 0) {
            return $this->success('添加成功', url('admin/cartoon/index'));
        } else {
            return $this->error('添加失败');
        }
    }


    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        // 是否为 Ajax 请求
        if (!Request::instance()->isAjax()) {
           return $this->error('你四不四迷路了!', '/');
        }

        // 显式查询
        $row = db('mulu')->find($id);
//        var_dump($row);
        if ($row === null) {
            $info['status'] = false;
            $info['name'] = '查无数据';
            $info['detail'] = '查无数据,请重试';
        } else {
            $info['status'] = true;
            $info['name'] = $row['name'];
            $info['detail'] = $row['detail'];
        }

        return json($info);

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $guanli = Session::get('adname');
        $data = db('zuopin')->find($id);
        return view('cartoon/cartoonedit',[
            'guanli'=>$guanli,
            'data'=>$data
        ]);
    }






    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function updated(Request $request, $id)
    {
//        $p = $request->put();
        $p = $request->post();

        $file = request()->file('pic');
        //移动到框架应用根目录/public/uploads/ 目录下
        if ($file){
              $info = $file->validate(['size'=>156780,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
//            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info){
//                输出jpg
//                echo $info->getExtension();
//                输出20180802/...jpg
//                echo $info->getSaveName();
                $p['pic'] = $info->getSaveName();
//                输出...jpg
//                echo $info->getFilename();
            }else{
//                失败信息
                echo $file->getError();
            }
        }
//        dump($p);die;
        if (empty($p['pic'])){
            $data = [
                'name' => $p['name'],
                'author' => $p['author'],
                'cid' => $p['cid'],
                'fid' => $p['fid'],
                'detail' => $p['detail'],
                'status' => $p['status']
            ];
        }else{
            $data = [
                'pic' => $p['pic'],
                'name' => $p['name'],
                'author' => $p['author'],
                'cid' => $p['cid'],
                'fid' => $p['fid'],
                'detail' => $p['detail'],
                'status' => $p['status']
            ];
        }

        $res = db('zuopin')->where('id','eq',$id)->update($data);

        if ($res) {
            return $this->success('编辑成功', url('admin/cartoon/index'));
        } else {
            return $this->error('编辑失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = db('zuopin')->delete($id);
        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的漫画删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的漫画删除失败!';
        }
        // JSON 返回
        return json($info);
    }




}
